<template>
    <div>
        <Title :title="title"></Title>
        <div ref="bottom2" style="height: 150px;margin-top: 30px;"></div>
    </div>
</template>
<script>
import { Scatter } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import { mapState } from 'vuex';
export default {
    data() {
        return {
            title: '噪音回归线',
            params: {
                page: 3,
                pageSize: 300
            },
            zData: [],
            scatter: null
        }
    },
    computed: {
        ...mapState(['ed'])
    },
    watch: {
        ed: {
            handler(n, o) {
                // 根据设备信息重新查询数据 重新渲染图表 
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getZData();
                // 根据最新获取的数据重新渲染图表 
                this.scatter.changeData(this.zData);
            },
            // 深度监听 
            deep: true
        }
    },
    methods: {
        loadScatter() {
            this.scatter = new Scatter(this.$refs.bottom2, {
                data: this.zData,
                xField: 'insert_time',
                yField: 'noise',
                size: 5,
                pointStyle: {
                    stroke: 'green',
                    lineWidth: 1,
                    fill: 'pink',
                },
                regressionLine: {
                    type: 'quad', // linear, exp, loess, log, poly, pow, quad
                },
            });
            this.scatter.render();
        },
        async getZData() {
            let res = await new get('/dashboard/pageQueryData?', this.params);
            console.log(res, '84163');
            let temp = [];
            // 将后端获取的数据设置给tempData 
            res.data.list.forEach((item, index, arr) => {
                arr[0].noise = 34.8,
                    // item表示数组每一项 
                    item.insert_time = moment(item.insert_time).format("HH:mm");
                // 由于查询数据密度密集 需要将数据处理 每隔10条数据取一条数据 
                if (index % 10 == 0) {
                    temp.push(arr[index])
                }
            });
            this.zData = temp;
        }
    },
    async mounted() {
        await this.getZData();
        this.loadScatter();
    }
}
</script>
<style lang="less" scoped></style>